﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TrackingDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ITrackingDetailDA TrackingDetailDA = DataAccess.CreateTrackingDetailDA();

        public TrackingDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public TrackingDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TrackingDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<TrackingDetailInfo> trackingDetailInfos = new List<TrackingDetailInfo>();
                TrackingDetailInfo trackingDetailInfo = null;
                foreach (string id in IDS)
                {
                    trackingDetailInfo = TrackingDetailDA.GetTrackingDetailByID(dataBase, tran, id, this.dataBaseID[0]);
                    trackingDetailInfos.Add(trackingDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (TrackingDetailDA.DeleteTrackingDetail(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TrackingDetailInfo>(dataBase, tran, trackingDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DRIVER", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return TrackingDetailDA.GetAllTrackingDetail(this.dataBaseID[0]);
        }

        public TrackingDetailInfo GetInfoByID(string trackingDetailID)
        {
            return TrackingDetailDA.GetTrackingDetailByID(trackingDetailID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(TrackingDetailQueryEntity trackingDetailQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(trackingDetailQuery.BolID))
            {
                sqlWhere.Add(" td.BOL_ID " + trackingDetailQuery.BolID);
            }
            return TrackingDetailDA.GetTrackingDetailByQueryList(sqlWhere, trackingDetailQuery, this.dataBaseID[0]);
        }

        public DataSet GetTrackingByQueryList(TrackingDetailQueryEntity trackingDetailQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(trackingDetailQuery.BolID))
            {
                sqlWhere.Add(" b.BOL_ID " + trackingDetailQuery.BolID);
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.DeliveryOrderID))
            {
                sqlWhere.Add(" bd.DELIVERY_ID " + trackingDetailQuery.DeliveryOrderID);
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.EquipmentLicence))
            {
                sqlWhere.Add(" b.TRANSPORT_EQUIPMENT_ID " + trackingDetailQuery.EquipmentLicence);
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.CarrierID))
            {
                sqlWhere.Add(" te.CARRIER_ID " + trackingDetailQuery.CarrierID);
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.DriverID))
            {
                sqlWhere.Add(" b.DRIVER_ID " + trackingDetailQuery.DriverID);
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.DriverPhone))
            {
                sqlWhere.Add(" d.TELEPHONE " + trackingDetailQuery.DriverPhone);
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.TrackingDate))
            {
                sqlWhere.Add(trackingDetailQuery.TrackingDate.Replace("@TrackingDate@", " td.TRACKING_DATE "));
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.CreatedBy))
            {
                sqlWhere.Add(" b.CREATED_BY " + trackingDetailQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(trackingDetailQuery.Status))
            {
                sqlWhere.Add(" bd.STATUS " + trackingDetailQuery.Status);
            }
            return TrackingDetailDA.GetTrackingByQueryList(sqlWhere, trackingDetailQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string trackingDetailID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                TrackingDetailInfo trackingDetailInfo = obj as TrackingDetailInfo;
                if (!string.IsNullOrEmpty(trackingDetailInfo.TrackingID))
                {
                    if (TrackingDetailDA.CheckTrackingDetailIDUnique(dataBase, tran, trackingDetailInfo.TrackingID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8400_001", this.pubParasInfo.UserLanguage, new List<string> { trackingDetailInfo.TrackingID });
                        throw new Exception("TrackingID'" + trackingDetailInfo.TrackingID + "'已经存在! 请重新输入!");
                    }
                }
                else
                {
                    trackingDetailInfo.TrackingID = commonMethod.GenNextNumStr(dataBase, tran, "TRACKING_DETAIL", this.dataBaseID[0]);
                }
                trackingDetailInfo.UpdatedBy = trackingDetailInfo.CreatedBy;
                trackingDetailInfo.CreatedDate = DateTime.Now;
                trackingDetailInfo.UpdatedDate = DateTime.Now;
                if (TrackingDetailDA.InsertTrackingDetail(dataBase, tran, trackingDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TrackingDetailInfo>(dataBase, tran, null, trackingDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TRACKING_DETAIL", this.dataBaseID[0]);
                    trackingDetailID = trackingDetailInfo.TrackingID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return trackingDetailID;
        }

        public string Update(object obj)
        {
            string trackingDetailID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                TrackingDetailInfo trackingDetailInfo = obj as TrackingDetailInfo;
                trackingDetailInfo.UpdatedDate = DateTime.Now;
                TrackingDetailInfo oldTrackingDetailInfo = TrackingDetailDA.GetTrackingDetailByID(dataBase, tran, trackingDetailInfo.TrackingID, this.dataBaseID[0]);
                if (TrackingDetailDA.UpdateTrackingDetail(dataBase, tran, trackingDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TrackingDetailInfo>(dataBase, tran, oldTrackingDetailInfo, trackingDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "TRACKING_DETAIL", this.dataBaseID);
                    trackingDetailID = trackingDetailInfo.TrackingID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return trackingDetailID;
        }
    }
}

